
let changeValue = (value) => {
    if (!value) {
        return null
    } else {
        return value.constructor === Number ? value : `"${value}"`
    }
}

let getCode = (data, msg, errcode = 200) => {
    return {
        data,
        msg,
        errcode
    }
}
let getHaveData = (data) => {
    let sqlText = ''
    let haveDataList = Object.keys(data).filter((item) => {
        return data[item] != undefined
    })
    if (haveDataList.length > 0) {
        haveDataList.map((item, index) => {
            sqlText += item + ' = ' + changeValue(data[item]) + (index != haveDataList.length - 1 ? ' and ' : ' ')
        })
        sqlText = 'where ' + sqlText
    }
    return sqlText
}
let addData = (data) => {
    let listValue = Object.values(data).map((value) => {
        return changeValue(value)
    })
    return `(${Object.keys(data)}) values (${listValue})`
}
let updataData = (data) => {
    let list = Object.keys(data).map((item) => {
        return `${item} = ${changeValue(data[item])}`
    })
    return list.join(',')
}

module.exports = {
    getCode,
    addData,
    getHaveData,
    updataData
}